<?php

class Facebook extends CI_Controller {
	
	 public function __construct(){
		parent::__construct();		
		if(!$this->session->userdata('admin'))
			redirect(base_url('admin/login'));
		$this->load->model('Facebook_m', 'facebook');
    }
	
	function index() {
		$data = array();
		//pagination settings
		$config['per_page'] = 10;
		$config['base_url'] = base_url('admin/facebook');
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = 10;
		$config['full_tag_open'] = '<ul>';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a>';
		$config['cur_tag_close'] = '</a></li>';		
		$paged = $this->uri->segment(3);
		$paged = ($paged * $config['per_page']) - $config['per_page'];
		if ($paged < 0){
			$paged = 0;			
		}
		//filter post data
		$post = $this->input->post();
		if($post){
			$this->form_validation->set_rules('name', 'name', 'trim');
			$this->form_validation->set_rules('active', 'active', 'trim');
			$this->form_validation->run();
			$filter['name']	=	$this->input->post('name');
			$filter['active']	=	$this->input->post('active');
			$this->session->set_userdata('seo_filter', $filter);
		}else if($paged == 0)
			$this->session->unset_userdata('seo_filter');
		$tmp = array(
			'limit'		=>	$config['per_page'],
			'paged'		=>	$paged,			
		);
		//filter
		$filter = $this->session->userdata('seo_filter');
		if(!$filter) $filter = array();
		$filter = array_merge($filter, $tmp);
		//
		$config['total_rows'] = $this->facebook->get_all_active($filter, true);
		// echo $this->db->last_query();die;
		$data['accounts'] = $this->facebook->get_all_active($filter);
		$page = $this->uri->segment(3);
		$page = $page > 1 ? $page - 1 : 0;
		$data['stt']		= $page * $config['per_page'] + 1;		
		//initializate the panination helper 
      $this->pagination->initialize($config);  
		$data['main_content'] = 'admin/facebook';
      $this->load->view('admin/includes/template', $data);
		// die('aabc');
	}
	
	function queue(){
		$data = array();
		//pagination settings
		$config['per_page'] = 10;
		$config['base_url'] = base_url('admin/facebook/queue');
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = 10;
		$config['full_tag_open'] = '<ul>';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a>';
		$config['cur_tag_close'] = '</a></li>';		
		$paged = $this->uri->segment(4);
		$paged = ($paged * $config['per_page']) - $config['per_page'];
		if ($paged < 0){
			$paged = 0;			
		}
		//filter post data
		$post = $this->input->post();
		if($post){
			$this->form_validation->set_rules('fb_nick', 'fb_nick', 'trim');
			$this->form_validation->set_rules('status', 'status', 'trim');
			$this->form_validation->run();
			$filter['fb_nick']	=	$this->input->post('fb_nick');
			$filter['status']	=	$this->input->post('trang_thai');
			$this->session->set_userdata('seo_filter', $filter);
		}else if($paged == 0)
			$this->session->unset_userdata('seo_filter');
		$tmp = array(
			'limit'		=>	$config['per_page'],
			'paged'		=>	$paged,			
		);
		//filter
		$filter = $this->session->userdata('seo_filter');
		if(!$filter) $filter = array();
		$filter = array_merge($filter, $tmp);
		//
		$config['total_rows'] = $this->facebook->get_files_queue($filter, true);
		// echo $this->db->last_query();die;
		$data['files']	= $this->facebook->get_files_queue($filter);
		$page = $this->uri->segment(4);
		$page = $page > 1 ? $page - 1 : 0;
		$data['stt']		= $page * $config['per_page'] + 1;		
		//initializate the panination helper 
      $this->pagination->initialize($config);		
		$data['main_content'] = 'admin/facebook_queue';
      $this->load->view('admin/includes/template', $data);
		return;		
	}
	
	function upload(){
		$config['upload_path'] = './queue/';
		$config['allowed_types'] = '*';
		// $config['max_size']	= '100';
		// $config['max_width']  = '1024';
		// $config['max_height']  = '768';

		$this->load->library('upload', $config);

		if (!$this->upload->do_upload()){
			$data = array();
			$data['error'] = $this->upload->display_errors();
			$data['main_content'] = 'admin/facebook_queue';
			$this->load->view('admin/includes/template', $data);
		}
		else{
			$fb_nick	= $this->input->post('fb_nick');
			$upload = $this->upload->data();
			date_default_timezone_set('Asia/Bangkok');		
			$this->facebook->save_queue(array(
				'fb_nick'	=>	$fb_nick,
				'file_name'	=>	$upload['orig_name'],
				'created'	=> date('Y-m-d H:i:s'),
				'status'		=>	'C'
			));
			$id = $this->db->insert_id();
			// echo '<pre>';
			// print_r($upload);die;
			$html = file_get_html("queue/{$upload['orig_name']}");
			// $html = file_get_html('assets/queue/exam2_long.html');
			// $target = $html->find('ul._262m');
			if($html){
				 $target = $html->find('li._698');
				 $items = array();
				 foreach($target as $obj){
					$item = array();
					$tmp = $obj->find('a._5q6s');
					if(count($tmp))
						$tmp = $tmp[0]->getAttribute('href');
					else
						$tmp = 'Lỗi href';
					$item['link'] = htmlspecialchars($tmp);
					$item['code'] = md5($item['link']);				
					$item['fb_nick'] = $fb_nick;
					$tmp = $obj->find('img._s0');
					if(count($tmp))
						$tmp = $tmp[0]->getAttribute('src');
					else
						$tmp = 'Lỗi src';
					$item['thumb_uri'] = html_entity_decode($tmp);
					$item['thumb']	= "{$item['code']}.jpg";
					$thumb = "thumb/{$item['thumb']}";
					file_put_contents($thumb, file_get_contents($item['thumb_uri']));
					$tmp = $obj->find('div.fcb');
					if(count($tmp))
						$tmp = $tmp[0]->first_child()->innertext;
					else
						$tmp = 'Lỗi tên';
					$item['name'] = $tmp;
					$item['created'] = date('Y-m-d');
					array_push($items, $item);
					if(count($items) == 10){
						if($this->facebook->insert_batch($items))
							$items = array();
					}
				 }
				if(count($items))
					$this->facebook->insert_batch($items);
				$this->facebook->save_queue(array(
					'id'			=>	$id,
					'status'		=>	'X'
				));
				// echo $this->db->last_query();die;
				 // redirect('admin/facebook');
				 // echo count($items);
				 // var_dump($items);die;
			}
			redirect('admin/facebook/queue');
		}
	}
}